package com.ruoyi.duty.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.duty.domain.DutyJypx;
import com.ruoyi.duty.domain.bo.DutyJypxBo;
import com.ruoyi.duty.domain.vo.DutyJypxVo;
import com.ruoyi.duty.mapper.DutyJypxMapper;
import com.ruoyi.duty.service.IDutyJypxService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 就业培训Service业务层处理
 *
 * @author xiaohao
 * @date 2023-01-19
 */
@RequiredArgsConstructor
@Service
public class DutyJypxServiceImpl implements IDutyJypxService {

    private final DutyJypxMapper baseMapper;

    /**
     * 查询就业培训
     */
    @Override
    public DutyJypxVo queryById(Long jypxId) {
        return baseMapper.selectVoById(jypxId);
    }

    /**
     * 查询就业培训列表
     */
    @Override
    public TableDataInfo<DutyJypxVo> queryPageList(DutyJypxBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DutyJypx> lqw = buildQueryWrapper(bo);
        Page<DutyJypxVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询就业培训列表
     */
    @Override
    public List<DutyJypxVo> queryList(DutyJypxBo bo) {
        LambdaQueryWrapper<DutyJypx> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DutyJypx> buildQueryWrapper(DutyJypxBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DutyJypx> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPxsj() != null, DutyJypx::getPxsj, bo.getPxsj());
        return lqw;
    }

    /**
     * 新增就业培训
     */
    @Override
    public Boolean insertByBo(DutyJypxBo bo) {
        DutyJypx add = BeanUtil.toBean(bo, DutyJypx.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setJypxId(add.getJypxId());
        }
        return flag;
    }

    /**
     * 修改就业培训
     */
    @Override
    public Boolean updateByBo(DutyJypxBo bo) {
        DutyJypx update = BeanUtil.toBean(bo, DutyJypx.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DutyJypx entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除就业培训
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
